package com.xbts.web.controller.cloud;

import java.util.List;

import com.xbts.framework.config.ServerConfig;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.xbts.common.annotation.Log;
import com.xbts.common.core.controller.BaseController;
import com.xbts.common.core.domain.AjaxResult;
import com.xbts.common.enums.BusinessType;
import com.xbts.system.domain.CnInvoiceManagement;
import com.xbts.system.service.ICnInvoiceManagementService;
import com.xbts.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * 发票管理Controller
 *
 * @author xbts
 * @date 2023-08-03
 */
@Api("发票管理")
@RestController
@RequestMapping("/system/invoiceManagement")
public class CnInvoiceManagementController extends BaseController {

    @Autowired
    private ICnInvoiceManagementService cnInvoiceManagementService;
    @Autowired
    private ServerConfig serverConfig;

    /**
     * 查询发票
     */
    @ApiOperation("查询发票")
        @PreAuthorize("@ss.hasPermi('system:invoiceManagement:list')")
    @PostMapping("/list")
    public TableDataInfo getInvoiceManagementList(@RequestBody CnInvoiceManagement cnInvoiceManagement) {
        startPage(cnInvoiceManagement);
        List<CnInvoiceManagement> cnInvoiceManagements = cnInvoiceManagementService.selectCnInvoiceManagement(cnInvoiceManagement);
        return getDataTable(cnInvoiceManagements);
    }

    /**
     * 管理端新增发票内容
     */
    @ApiOperation("管理端新增发票内容")
    @PreAuthorize("@ss.hasPermi('system:invoiceManagement:add')")
    @ApiImplicitParam(name = "cnInvoiceManagement", value = "发票", required = true, dataType = "String", paramType = "path", dataTypeClass=String.class)
    @Log(title = "管理端新增发票内容", businessType = BusinessType.INSERT)
    @PostMapping("/insert")
    public AjaxResult add(@Validated @RequestBody CnInvoiceManagement cnInvoiceManagement) {
        return toAjax(cnInvoiceManagementService.insertCnInvoiceManagement(cnInvoiceManagement));
    }


    /**
     * 删除发票
     */
    @ApiOperation("删除发票")
    @PreAuthorize("@ss.hasPermi('system:invoiceManagement:remove')")
    @Log(title = "删除发票内容", businessType = BusinessType.DELETE)
    @ApiImplicitParam(name = "invoiceManagementId", value = "发票id", required = true, dataType = "String", paramType = "path", dataTypeClass=String.class)
    @PostMapping("/delete")
    public AjaxResult deleteContent(Long invoiceManagementId) {
        return toAjax(cnInvoiceManagementService.deleteContentById(invoiceManagementId));
    }

    /**
     * 批量删除发票
     */
    @ApiOperation("批量删除发票")
    @PreAuthorize("@ss.hasPermi('system:invoiceManagement:remove')")
    @Log(title = "批量删除发票", businessType = BusinessType.DELETE)
    @PostMapping("/batchDelete")
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        return toAjax(cnInvoiceManagementService.batchDelete(ids));
    }


    /**
     * 更新发票内容
     */
    @ApiOperation("更新发票内容")
    @PreAuthorize("@ss.hasPermi('system:invoiceManagement:edit')")
    @Log(title = "更新发票内容", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult updateContent(@RequestBody CnInvoiceManagement cnInvoiceManagement, MultipartFile file) {
        return toAjax(cnInvoiceManagementService.updateInvoiceManagement(cnInvoiceManagement,file));
    }

    /**
     * 用户申请开票
     */
    @ApiOperation("用户申请开票")
    @PreAuthorize("@ss.hasPermi('system:invoiceManagement:edit')")
    @Log(title = "用户申请开票", businessType = BusinessType.UPDATE)
    @PostMapping("/applyForInvoicing")
    public AjaxResult applyForInvoicing(@RequestBody CnInvoiceManagement cnInvoiceManagement) {
        return toAjax(cnInvoiceManagementService.applyForInvoicing(cnInvoiceManagement));
    }

    /**
     * 后端处理发票
     */
    @ApiOperation("后端处理发票")
    @PreAuthorize("@ss.hasPermi('system:invoiceManagement:edit')")
    @Log(title = "后端处理发票", businessType = BusinessType.UPDATE)
    @ApiImplicitParam(name = "url,id", value = "文件地址，发票id", required = true, dataType = "String", paramType = "path", dataTypeClass = String.class)
    @PostMapping("/upload")
    public AjaxResult uploadFile(String url, Long invoiceId, MultipartFile file) {
        return toAjax(cnInvoiceManagementService.updateFileUrl(url, invoiceId, file));
    }

    /**
     * 发票下载
     */
    @ApiOperation("发票下载")
    @Log(title = "发票下载", businessType = BusinessType.EXPORT)
    @PostMapping("/download")
    public void download(HttpServletResponse response, @RequestBody CnInvoiceManagement cnInvoiceManagement) {
        cnInvoiceManagementService.download(response, cnInvoiceManagement);
    }

    /**
     * 邮件发送测试
     * @param email 邮箱号
     * @param fileUrl 文件地址
     */
    @GetMapping("/test/sendEmail")
    public void testSendEmail(String email,String fileUrl) {
        cnInvoiceManagementService.testSendEmail(email, fileUrl);
    }

}
